package cn.icybear.orzjisp;

import cn.icybear.orzjisp.bytefile.OrzConstants;

public class Constant implements Value, OrzConstants {
	public static final Constant BOOL_TRUE = new Constant(OrzConstants.TRUE);
	public static final Constant BOOL_FALSE = new Constant(OrzConstants.FALSE);
	public static final Constant OBJ_NULL = new Constant(null);
	public static Constant getConstant(boolean b) {
		return b ? BOOL_TRUE : BOOL_FALSE;
	}

	public static Constant getConstant(Object v) {
		if (v == null)
			return OBJ_NULL;
		if (v instanceof Boolean)
			return getConstant(Boolean.class.cast(v).booleanValue());
		return new Constant(v);
	}

	public Object value;

	public Constant(Object val) {
		this.value = val;
	}

	@Override
	public Object getValue() {
		return value;
	}

	@Override
	public String toString() {
		return "Constant:" + (value == null ? "null" : value.toString());
	}

	@Override
	public int hashCode() {
		return value==null?0:value.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Constant other = (Constant) obj;
		if (value == other.value) {
			return true;
		} else if (value!=null&&value.equals(other.value))
			return true;
		return false;
	}

}
